<%@ page language="java" contentType="text/html; charset=UTF-8"
		 pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<base href="<%=basePath%>">
<html lang="zn-cn">
<head>
	<meta charset="UTF-8">
	<meta name="viewport"content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
	<meta name="format-detection" content="telephone=no" />
	<title>添加组织</title>
	<link rel="stylesheet"href="css/service.css">
	<style type="text/css">
		.excuse{
			width:100%;
			height:100px;
			border-radius:1px;
			resize:none;
			padding: 10px;
		}
		.excuseDiv{
			text-align: center;
			margin-bottom: 20px;
			margin-top: 30px;
		}
	</style>

	<script type="text/javascript"src="js/jquery.min.js"></script>
	<script type="text/javascript"src="js/base.js"></script>
	<script type="text/javascript" src="js/common.js"></script>
	<script type="text/javascript" src="js/handle.js"></script>
	<script type="text/javascript" src="js/ask.js"></script>
</head>
<body class="ed">
<div class="wrap">
	<div class="inputcardnum" >
		<img src="img/add_top.png">
		<p>
			Join-
			<input type="text" placeholder="请输入组织唯一标示" maxlength="30" class="cardNumInput"></p>
		<h2 class="bindCard">确认加入</h2>
	</div>
	<div class="checkphone " style="display:none">
		<div class="excuseDiv">
			<textarea class="excuse" ></textarea>
		</div>

		<div class="input_wrap">
			<div class="codebox"> <i>邀请人</i>
				<input type="tel" class="codeNum" placeholder="请输入邀请人" maxlength="300">
			</div>
		</div>
		<div class="tipsbox">
			<p>验证码已发送！</p>
		</div>
		<h3>
			温馨提示：该组织为不公开组织，
			<br/>需发送申请由管理员添加！
		</h3>
		<h2 class="certify">发送申请</h2>
	</div>
	<div class="success" style="display:none">
		<img src="img/add_success.png">
		<p id="tips">加入成功！</p>
		<h2 class="goBack">完成</h2>
	</div>
</div>
<div class="toast toast_timeout" style="display:none">网络异常，请稍后重试！</div>
<div class="toast toast_error" style="display:none">信息输入有误</div>
<div class="loading" style="display:none">
	<img src="img/loading.gif"></div>
<script type="text/javascript">

//    $(".inputcardnum").hide();
//    $(".success").show();
    var orgMark ="";
    var oid ="";
    var fromPage = "${fromPage}"; // 记录上一个页面
	console.log("fromPage:"+fromPage);
    // 卡有效性检验

    $(function () {
        //点击绑卡时，进入卡校验流程
        $(".bindCard").on('click', cardVerify);
        //点击验证时，进入绑卡流程
        $(".certify").on('click', sendApply);
        //返回卡管理页
        $(".goBack").on('touchend', function(e) {
            console.log("touchend");
            wx.closeWindow()
        });
    })
    function cardVerify() {
        console.log('step into cardVerify()');
        orgMark = $(".cardNumInput").val();
        if(orgMark == ''){
            errorToast("mark不能为空");
            return;
        }
        $(".loading").show();
        var data ={
            mark:orgMark
		}
        $.ajax({
            url:  $.httpRootPath() + 'rest/group/relation',
            type: 'GET',
            dataType: 'json',
            data: data,
            cache: false,
            timeout: 15000,
            success: function (r) {
                oid = r.data.id;
                console.log(r);
                $(".loading").hide();
                if (r.code == "00" || r.code =="10") {
                    errorToast(r.msg, 3);
                    return false;
                }else {
                    console.log("r.data.type"+r.data.type);
                    if(r.data.type == 0){
                        errorToast("该组织为私有组织,需申请加入");
                        $(".inputcardnum").hide();
                        $(".checkphone").show();
                        return;
					}else {
                        bindCard(r.data.type);
					}
				}
            },
            error: function (e) {
                $(".loading").hide();
                ajaxErrorEvent(e);
            }
        });
    }

    // 绑卡
    function bindCard(type) {
        var tips = type== 1?"加入成功":"关注成功";
        console.log('step into bindCard()');
        $(".loading").show();
        var data = {
            mark: orgMark,
        };
        console.log(data);
        $.ajax({
            url:  $.httpRootPath() + 'rest/group/join',
            type: 'POST',
            dataType: 'json',
            data: data,
            cache: false,
            timeout: 15000,
            success: function (r) {
                console.log(r);
                $(".loading").hide();
                if (r.code != "00") {
                    errorToast(r.msg, 3);
                    return false;
                }
                $("#tips").html(tips);
                $(".inputcardnum").hide();
                $(".success").show();
            },
            error: function (e) {
                $(".loading").hide();
                ajaxErrorEvent(e);
            }
        });
    }

    function sendApply() {
        console.log('step into sendApply()');
        var ex = $(".excuse").val();
        if (ex ==""){
            errorToast("申请理由不能为空",2);
            return;
		}
        var data ={
            oid:oid,
			excuse:ex,
			content:$(".codeNum").val()
        }
        console.log(data);
        $.ajax({
            url:  $.httpRootPath() + 'rest/group/sendApply',
            type: 'POST',
            dataType: 'json',
            data: data,
            cache: false,
            timeout: 15000,
            success: function (r) {
                console.log(r);
                $(".loading").hide();
                if (r.code != "00") {
                    errorToast(r.msg, 3);
                    return false;
                }
                $("#tips").html("申请发送成功");
                $(".checkphone").hide();
                $(".success").show();
            },
            error: function (e) {
                $(".loading").hide();
                ajaxErrorEvent(e);
            }
        });
    }

</script>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript" src="js/weixin.js"></script>
<script type="text/javascript">
    getSignature(['closeWindow'],function () {

    })
</body>
</html>